//
//  PasswordForgetViewController.m
//  WisdomZhuHai
//
//  Created by JasonLee on 15/2/1.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "PasswordForgetViewController.h"
#import "TransforController.h"
#import "Common.h"

@interface PasswordForgetViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *view_table;

@property (nonatomic, strong) UITextField *txt_email;

@end

@implementation PasswordForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
    [self setGesture];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void) initView{
    UIView *view_navigationBar = [TransforController shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:0 andTitle:@"邮箱找回密码"];
    [self.view addSubview:view_navigationBar];
    
    [self.view setFrame:[UIScreen mainScreen].bounds];
//    self.view_table = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - HEIGHT_NAVIGATION_BAR - HEIGHT_STATUS_BAR) style:UITableViewStyleGrouped];
    self.view_table = [[UITableView alloc]initWithFrame:[TransforController getRectOfView:self.view withHasNavigationBar:YES andHasBottomBar:NO] style:UITableViewStyleGrouped];
    self.view_table.delegate = self;
    self.view_table.dataSource = self;
    [self.view addSubview:self.view_table];
    
    UIBarButtonItem *btn_bar_right_send = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(sendRequest)];
    self.navigationItem.rightBarButtonItem = btn_bar_right_send;
}

- (void) setGesture{
    UITapGestureRecognizer *defaultTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(defaultSingleTapAction:)];
    //设置手势属性
    defaultTapGestureRecognizer.numberOfTapsRequired = 1;//设置点按次数，默认为1，注意在iOS中很少用双击操作
    defaultTapGestureRecognizer.numberOfTouchesRequired = 1;//点按的手指数
    //添加手势到对象(注意，这里添加到了控制器视图中，而不是图片上，否则点击空白无法隐藏导航栏)
    [self.view addGestureRecognizer:defaultTapGestureRecognizer];
}

- (void)defaultSingleTapAction:(UITapGestureRecognizer *)gesture{
    [self.txt_email resignFirstResponder];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"请输入邮箱来找回密码";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"txt_email"];
    self.txt_email = [[UITextField alloc]initWithFrame:CGRectMake(20, 0, cell.frame.size.width - 40, cell.frame.size.height)];
//    self.txt_email.borderStyle = UITextBorderStyleLine;
    self.txt_email.clearButtonMode = UITextFieldViewModeWhileEditing;
    [cell addSubview:self.txt_email];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)sendRequest{
    NSString *string_temp = self.txt_email.text;
    if([string_temp isEqualToString:@""]){
        [[Common shareInstance]showAlertViewWithTitle:nil text:@"请填写邮箱地址" dismissTime:0.8];
        return;
    }
    if([TransforController validateEmail:string_temp]){
        [[ProtocolManager shareInstance]sendVerifyMailWithMail:self.txt_email.text andTarget:self andSelector:@selector(sendRequestCallBack:)];
    }else{
        [[Common shareInstance]showAlertViewWithTitle:nil text:@"邮件格式不正确" dismissTime:0.8];
        return;
    }
}

- (void)sendRequestCallBack:(NSDictionary *)dic{
    if(dic == nil){
        return;
    }
    if([[Common valueForKey:@"respCode" dic:dic]isEqualToString:@"000"]){
        [[Common shareInstance]showAlertViewWithTitle:nil text:@"邮件已发送，请查看邮箱" dismissTime:0.8];
        [TransforController umengEvent:@"PasswordForget" attributes:@{@"With" : @"E-mail", @"attr":self.txt_email.text} number:@(1) ct:@"900003"];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


@end
